package com.shqcjd.sinopecdemomobile.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.DefectManageBean;
import com.shqcjd.sinopecdemomobile.model.pojo.EquipmentInfoBean;
import com.shqcjd.sinopecdemomobile.model.pojo.EquipmentInfoInspectBean;
import com.shqcjd.sinopecdemomobile.ui.adapter.EqumentInfoAdapter;
import com.shqcjd.sinopecdemomobile.ui.adapter.EqumentInfoDefectAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.ToastUtils;
import com.shqcjd.sinopecdemomobile.util.UIUtils;
import com.shqcjd.sinopecdemomobile.widget.FillListview;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;


/**
 * 设备详情
 */
public class EquipmentInfoActivity extends BaseActivity {

    @BindView(R.id.tv_include_title)
    TextView tvIncludeTitle;
    @BindView(R.id.tv_include_finish)
    Button tvIncludeFinish;
    @BindView(R.id.lv_equipment_info_inspection)
    FillListview lvEquipmentInfoInspection;
    @BindView(R.id.lv_equipment_info_defect)
    FillListview lvEquipmentInfoDefect;
    @BindView(R.id.tv_equipment_info_inspect)
    TextView tvEquipmentInfoInspect;
    @BindView(R.id.tv_equipment_info_defect)
    TextView tvEquipmentInfoDefect;
    @BindView(R.id.tv_equipment_info_type)
    TextView tvEquipmentInfoType;
    @BindView(R.id.tv_equipment_info_name)
    TextView tvEquipmentInfoName;
    @BindView(R.id.tv_equipment_info_kks)
    TextView tvEquipmentInfoKks;
    @BindView(R.id.tv_equipment_info_state)
    TextView tvEquipmentInfoState;
    @BindView(R.id.iv_equipment_info)
    ImageView ivEquipmentInfo;
    @BindView(R.id.tv_inspection_more)
    TextView tvInspectionMore;
    @BindView(R.id.tv_defect_more)
    TextView tvDefectMore;


    private EqumentInfoAdapter equmentInfoAdapter;
    private EqumentInfoDefectAdapter equmentInfoDefectAdapter;
    private List<EquipmentInfoInspectBean.ListBean> recordList = new ArrayList<>();
    private List<DefectManageBean.ListBean> exceptionList = new ArrayList<>();
    private Context context;
    private String kks;
    private int eId;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_equipment;
    }

    @Override
    public void init() {
        super.init();
        kks = getIntent().getStringExtra("kks");
    }

    @Override
    public void initView() {
        super.initView();
        context = this;
        tvIncludeTitle.setText("设备详情");

        tvIncludeFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getInfoData();

    }

    private void getInfoData() {
        ApiService.equipmentInfoHttp(kks)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<EquipmentInfoBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData<EquipmentInfoBean> retData) {
                        if (retData.retCode == 10000 && retData.data != null) {
                            RequestOptions options = new RequestOptions()
                                    .placeholder(R.mipmap.ic_default_small)    //加载成功之前占位图
                                    .error(R.mipmap.ic_default_small)    //加载错误之后的错误图
//                                .override(153, 148)    //指定图片的尺寸
                                    .centerCrop();//指定图片的缩放类型为centerCrop （等比例缩放图片，直到图片的宽高都
                            Glide.with(context)
                                    .load(retData.data.img)
                                    .apply(options)
                                    .into(ivEquipmentInfo);

                            tvEquipmentInfoType.setText("设备类型: " + retData.data.equipment_type_name);
                            tvEquipmentInfoName.setText("设备名称: " + retData.data.equipment_name);
                            tvEquipmentInfoKks.setText("KKS: " + retData.data.kks);
                            tvEquipmentInfoState.setText("状态: " + (retData.data.running ? "运行" : "未运行"));
                            eId = retData.data.id;
                            recordIsNull();

                            getInspectData();
                            getExpertData();
                        } else {
                            ToastUtils.showShort(context, "没有该设备信息");
                        }

                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                        ToastUtils.showShort(context, errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    private void getInspectData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageIndex", "1");
        map.put("pageSize", "8");
        map.put("equipmentId", eId + "");
        ApiService.equipmentInfoInspectRecordHttp(new JSONObject(map))
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<EquipmentInfoInspectBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<EquipmentInfoInspectBean> responseData) {
                        recordList = responseData.data.list;

                        equmentInfoAdapter = new EqumentInfoAdapter(recordList, context);
                        lvEquipmentInfoInspection.setAdapter(equmentInfoAdapter);
                        UIUtils.setListViewHeightBasedOnChildren(lvEquipmentInfoInspection);

                        recordIsNull();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    private void getExpertData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageIndex", "1");
        map.put("pageSize", "8");
        map.put("equipmentId", eId + "");
        ApiService.defectManageHttp(new JSONObject(map))
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<DefectManageBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData<DefectManageBean> responseData) {
                        exceptionList = responseData.data.list;

                        equmentInfoDefectAdapter = new EqumentInfoDefectAdapter(exceptionList, context);
                        lvEquipmentInfoDefect.setAdapter(equmentInfoDefectAdapter);
                        UIUtils.setListViewHeightBasedOnChildren(lvEquipmentInfoDefect);

                        recordIsNull();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    /**
     * 假如没有记录  显示暂无记录
     */
    private void recordIsNull() {
        if (recordList.size() == 0) {
            tvEquipmentInfoInspect.setText("巡检记录(暂无记录)");
        } else {
            tvEquipmentInfoInspect.setText("巡检记录");
        }
        if (exceptionList.size() == 0) {
            tvEquipmentInfoDefect.setText("缺陷记录(暂无记录)");
        } else {
            tvEquipmentInfoDefect.setText("缺陷记录");
        }
    }


    @OnClick({R.id.tv_inspection_more, R.id.tv_defect_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_inspection_more://更多巡检:
                Intent it = new Intent(this, InspectRecordActivity.class);
                it.putExtra("eId", eId);
                startActivity(it);
                break;
            case R.id.tv_defect_more://更多缺陷
                Intent its = new Intent(this, DefectManageActivity.class);
                its.putExtra("kks", kks);
                startActivity(its);
                break;
        }
    }
}
